package servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class servletGameRecord
 */
public abstract class BaseRecordsAbstractServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected static final String k_SuccessStr = "\"report\" : \"success\"";
	protected static final String k_FailStr = "\"report\" : \"fail\"";

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public BaseRecordsAbstractServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected final void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected final void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	private final void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;charset=UTF-8");
		String theAction = request.getParameter("action");
		
		if (theAction.equals("insert")) {
			processInsertRequest(request, response);
		} else if (theAction.equals("update")) {
			processUpdateRequest(request, response);
		} else if (theAction.equals("delete")) {
			processDeleteRequest(request, response);
		} else {
			processExtraRequest(request, response);
		}
	}

	protected void processExtraRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		// Inheriting classes are able to override this method to extend for further operations.
	}
	
	protected abstract void processInsertRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;

	protected abstract void processUpdateRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException; 

	protected abstract void processDeleteRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;	
}